package com.loveprogrammer.eternity.skill.support.skills;

import com.loveprogrammer.eternity.model.Character;
import com.loveprogrammer.eternity.common.Tuple;
import com.loveprogrammer.eternity.skill.support.states.ArmourIncrState;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @version 1.0.0
 * @description: 圣言术
 * @author: eric
 * @date: 2022-08-11 16:49
 **/
public class SkillHolyWord extends AbstractSkill {

    public SkillHolyWord(int id, int baseInterval, int level, int order, int maxLevel, String name, String desc) {
        super(id, baseInterval, level, order, maxLevel, name, desc);
    }

    @Override
    public boolean check(Character character) {
        return true;
    }

    @Override
    public int opportunityPassive() {
        // 0为 主动技能
        return 0;
    }

    @Override
    public boolean checkLevelUp(int characterLevel) {
        // 每两级升一级
        if(characterLevel % 3 == 0){
            return true;
        }
        return false;
    }

    /***
     * 发动技能
     **/
    @Override
    public Tuple<String, List<Character>> skillLaunch(Character character,
                                                      List<? extends Character> attack,
                                                      List<? extends Character> defences) {
        Tuple<String, List<Character>> result = new Tuple<>();
        // 给所有人
        StringBuilder builder = new StringBuilder();
        builder.append(character.getName()).append("(").append(character.getCurrentHp()).append("/")
                .append(character.getHpMax()).append(")").append(" 使用技能[")
                .append(getName()).append(" ").append(getLevel()).append("级】治疗了 ").append("所有己方目标\n");
        int power = getHarm(character);
        // 如果己方有生命值不满的情况，施加圣言术恢复生命力
        for (Character cha : attack) {
            int currentHp = cha.getCurrentHp();
            int hpMax = cha.getHpMax();
            if(currentHp < hpMax){
                int add = (currentHp + power) > hpMax ? (hpMax-currentHp) : power;
                builder.append("\t\t").append(cha.getName()).append("增加生命力").append(add).append("----(")
                        .append(cha.getCurrentHp() + add).append("/").append(cha.getHpMax()).append(")\n");
                cha.hp().addAndGet(add);
            }
            // 增加护甲
            int armIncr = getSecondHarm(character);
            ArmourIncrState armourIncrState = new ArmourIncrState(cha,defences,armIncr);
            if(cha.acquireState(armourIncrState, 10)) {
                cha.setArmature(character.getArmature() + armIncr);
                // setTimeTask(armourIncrState, 10);
                // 10秒后恢复
                timer.newTimeout(timeout -> {
                    armourIncrState.reset();
                }, 10, TimeUnit.SECONDS);
                builder.append("\t\t").append(cha.getName()).append("增加护甲").append(armIncr).append("点\n");
            }
        }
        result.setValue(new ArrayList<>());
        result.setKey(builder.toString());
        return result;
    }

    @Override
    protected int computeHarm(Character character) {
        return (int) (character.getMagic() * ((100 + getLevel() * 1.3) / 100));
    }

    @Override
    protected int computeSecondHarm(Character character) {
        return (int) (character.getTechnique() * 0.08);
    }
}
